package com.ruoyi.examination.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.examination.dto.QuestionImportDto;
import com.ruoyi.examination.vo.QuestionSelectVo;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.examination.domain.Question;
import com.ruoyi.examination.service.IQuestionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 试题管理Controller
 *
 * @author hcc
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController
{
    @Autowired
    private IQuestionService questionService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询试题管理列表
     */
    @RequiresPermissions("examination:question:list")
    @GetMapping("/list")
    public TableDataInfo list(Question question)
    {
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * 查询试题管理列表
     */
    @RequiresPermissions("examination:question:list")
    @GetMapping("/getList")
    public TableDataInfo getList(QuestionSelectVo questionSelectVo)
    {
        List<Question> list = questionService.getList(questionSelectVo);
        return getDataTable(list);
    }

    /**
     * 导出试题管理列表
     */
    @RequiresPermissions("examination:question:export")
    @Log(title = "试题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.selectQuestionList(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "试题管理数据");
    }

    /**
     * 获取试题管理详细信息
     */
    @RequiresPermissions("examination:question:query")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") String questionId)
    {
        return success(questionService.selectQuestionByQuestionId(questionId));
    }

    /**
     * 新增试题管理
     */
    @RequiresPermissions("examination:question:add")
    @Log(title = "试题管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Question question)
    {
        return toAjax(questionService.insertQuestion(question));
    }

    /**
     * 修改试题管理
     */
    @RequiresPermissions("examination:question:edit")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除试题管理
     */
    @RequiresPermissions("examination:question:remove")
    @Log(title = "试题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable String[] questionIds)
    {
        return toAjax(questionService.deleteQuestionByQuestionIds(questionIds));
    }

    /**
     * 关联题组
     */
    @RequiresPermissions("examination:question:edit")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping("/associationGroup")
    public AjaxResult associationGroup(@RequestBody Question question)
    {
        return toAjax(questionService.associationGroup(question));
    }

    /**
     * 批量逻辑删除试题管理
     */
    @RequiresPermissions("examination:question:edit")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping("/removeQuestion/{questionIds}")
    public AjaxResult removeQuestion(@PathVariable String[] questionIds)
    {
        return toAjax(questionService.removeQuestion(questionIds));
    }



    /**
     * 导入试题管理
     */
    @RequiresPermissions("examination:question:import")
    @Log(title = "试题管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<QuestionImportDto> util = new ExcelUtil<QuestionImportDto>(QuestionImportDto.class);
        List<QuestionImportDto> questionList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = questionService.importQuestion(questionList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 查看题组已关联题目
     */
    @RequiresPermissions("examination:question:list")
    @GetMapping("/viewQuestion")
    public TableDataInfo viewQuestion(QuestionSelectVo questionSelectVo)
    {
        List<Question> list = questionService.viewQuestion(questionSelectVo);
        return getDataTable(list);
    }
}
